package LeetCode.leetcode349;

import java.util.*;
import java.util.logging.Handler;

public class Main {
    public static void main(String[] args) {
        int[] arr1 = {1, 2, 2, 3};
        int[] arr2 = {2, 2};
        int[] arr3 = intersection(arr1, arr2);
        System.out.println(Arrays.toString(arr3));
    }

    /**
     *
     * @param nums1
     * @param nums2
     * @return
     */
    public static int[] intersection(int[] nums1, int[] nums2) {
        ArrayList<Integer> arrayList = new ArrayList<>();
        HashSet<Integer> set1 = new HashSet<>();
        HashSet<Integer> set2 = new HashSet<>();
        for (int k : nums1) {
            set1.add(k);
        }
        for (int j : nums2) {
            set2.add(j);
        }
        int count = 0;
        int val;
        Iterator<Integer> iterator = set2.iterator();
        while (iterator.hasNext()) {
            val = iterator.next();
            if (set1.contains(val)) {
                count++;
                arrayList.add(val);
            }
        }
        int[] arr3 = new int[count];
        for (int i = 0; i < count; i++) {
            arr3[i] = arrayList.get(i);
        }
        return arr3;
    }
}
